
'use strict';

const fs = require('fs');

module.exports = {
  generateFunc, //生成函数
  writeFile,    //写文件
  handleReplace,  //模板替换替换函数
  testAndMakeDirectoryMore, //异步创建一组文件夹
  testAndMakeDirectory,  //创建一个文件夹
};

//替换
function handleReplace(tpl, opt) {
  return tpl.replace(/{{{(.*?)}}}/g, (node, key) => {
    return opt[key];
  })
}

//写入文件
function writeFile(opt) {
  var outputPath = opt.outputPath,
    fileContent = opt.fileContent;
  console.log(outputPath);
  const data = new Uint8Array(Buffer.from(fileContent));
  fs.writeFile(outputPath, data, (err) => {
    if (err) throw err;
    console.log('保存' + outputPath);
  });
}

// tool.writeFile({
//   outputPath: '../src/pages/admin/',
//   fileContent: loginTpl
// })

//创建文件夹函数 单个文件创建
function testAndMakeDirectory(opt) {
  var dirPath = opt.dirPath;
  return new Promise((resolve, reject) => {
    fs.readdir(dirPath, (err, file) => {
      //文件夹不存在
      if (err) {
        fs.mkdir(dirPath, (err2, data) => {
          if (err2) {
            console.log(err2);
          } else {
            resolve();
          }
        });
      } else {
      //文件夹存在
        resolve();
      }
    });
  });
}

// var makeDir = tool.testAndMakeDirectory({
//   dirPath: './src/router/router_group',
// });
// makeDir.then((item, index) => {

// })


//创建一组文件夹 多个文件创建
function testAndMakeDirectoryMore(opt) {
  var dirPathArr = opt.dirPathArr;
  var promiseArr = [];
  dirPathArr.forEach((dirPath, index) => {
    promiseArr.push(new Promise((resolve, reject) => {
      fs.readdir(dirPath, (err, file) => {
        if (err) {
          fs.mkdir(dirPath, (err2, data) => {
            if (err2) {
              console.log(err2);
            } else {
              resolve();
            }
          });
        } else {
          resolve();
        }
      });
    }));
  });
  return Promise.all(promiseArr);  //返回一个promise all结果
}

// var makeAllDir = tool.testAndMakeDirectoryMore({
//   dirPathArr: [
//     './src/router/router_group',
//   ]
// });
// makeAllDir.then((item, index) => {
// 
//  });


//代码生成 读取写入 vue文件生成
function generateFunc(opt, finalHandle) {
  var filePath = opt.filePath ? opt.filePath : '',
    outputPath = opt.outputPath ? opt.outputPath : '',
    templateCode = opt.templateCode ? opt.templateCode : '{{{templateCode}}}',
    dataCode = opt.dataCode ? opt.dataCode : '{{{dataCode}}}',
    styleCode = opt.styleCode ? opt.styleCode : '{{{styleCode}}}',
    mountedCode = opt.mountedCode ? opt.mountedCode : '{{{mountedCode}}}',
    methodsCode = opt.methodsCode ? opt.methodsCode : '{{{methodsCode}}}',
    tipsText = opt.tipsText ? opt.tipsText : '模块 >> 标题',
    methodsCodeFirst = opt.methodsCodeFirst ? opt.methodsCodeFirst : "{{{methodsCodeFirst}}}",
    methodsCodeSecond = opt.methodsCodeSecond ? opt.methodsCodeSecond : "{{{methodsCodeSecond}}}",

    //表格特有的方法
    tableDefaultMounted = opt.tableDefaultMounted ? opt.tableDefaultMounted : "{{{tableDefaultMounted}}}",
    tableDefaultMethods = opt.tableDefaultMethods ? opt.tableDefaultMethods : "{{{tableDefaultMethods}}}";

  const p1 = new Promise((resolve, reject) => {
    fs.readFile(filePath, 'utf-8', (err, data) => {
      if (err) {
        reject(err);
        throw err;
      }
      var indexCode = data.replace(/{{{(.*?)}}}/g, (node, key) => {
        return {
          templateCode,
          dataCode,
          styleCode,
          mountedCode,
          methodsCode,
          tipsText,

          methodsCodeFirst,  // 索引方法
          methodsCodeSecond, // 所有方法集合

          tableDefaultMounted, //表格默认函数
          tableDefaultMethods,
          
        }[key];
      });
      if (finalHandle) {
        indexCode = finalHandle(indexCode);
      }
      resolve(indexCode);
    });
  })

  p1.then((indexCode) => {
    const data = new Uint8Array(Buffer.from(indexCode));
    fs.writeFile(outputPath, data, (err) => {
      if (err) throw err;
      console.log('保存' + outputPath);
    });
  }).catch((err) => {
    console.log(err);
  })
}
//